package com.jy.oms.framework.thread;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 自定义线程池管理服务
 *
 * @author liuquan
 */
public final class CustomThreadPoolManager {
    private static final String customThreadPoolKey = "threadPool-";
    private static CustomThreadPoolManager instance = new CustomThreadPoolManager();
    private Map<String, CustomThreadPoolExecutor> customThreadPoolExecutorMap = new ConcurrentHashMap<>();

    private CustomThreadPoolManager() {
    }

    public static CustomThreadPoolManager getInstance() {
        return instance;
    }

    public Map<String, CustomThreadPoolExecutor> getCustomThreadPoolExecutorMap() {
        return customThreadPoolExecutorMap;
    }

    /**
     * 注册线程池
     */
    public void register(CustomThreadPoolExecutor threadPoolExecutor) {
        customThreadPoolExecutorMap.put(customThreadPoolKey + threadPoolExecutor.getPoolId(), threadPoolExecutor);
    }

    /**
     * 删除线程池
     */
    public void unregister(CustomThreadPoolExecutor threadPoolExecutor) {
        if (customThreadPoolExecutorMap.get(threadPoolExecutor.getPoolId()) != null) {
            customThreadPoolExecutorMap.remove(threadPoolExecutor.getPoolId());
        }
    }
}
